export default {
  namespaced: true,
  state: {
    menuList: [],
    childrenHref: [],
    breadcrumb: [],
    href: "",
    visitedViews: []
  },
  mutations: {
    setMenuList(state, menuList) {
      state.menuList = menuList;
    },
    emptyMenuList(state) {
      state.menuList = [];
    },
    setChildrenHref(state, childrenHref) {
      state.childrenHref = childrenHref;
    },
    emptyChildrenHref(state) {
      state.childrenHref = [];
    },
    setBreadcrumb(state, breadcrumb) {
      state.breadcrumb = breadcrumb;
    },
    emptyBreadcrumb(state) {
      state.breadcrumb = [];
    },
    setHref(state, href) {
      state.href = href;
    },
    emptyHref(state) {
      state.href = "";
    },
    setVisitedViews(state, visitedViews) {
      state.visitedViews = visitedViews;
    },
    emptyVisitedViews(state) {
      state.visitedViews = [];
    },
    DEL_VISITED_VIEW: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.href === view.href) {
          state.visitedViews.splice(i, 1);
          break;
        }
      }
    },
    DEL_OTHERS_VISITED_VIEWS: (state, view) => {
      state.visitedViews = state.visitedViews.filter(v => {
        return v.href === view.href;
      });
    }
  },
  actions: {
    delVisitedViews({ dispatch, state }, view) {
      return new Promise(resolve => {
        dispatch("delVisitedView", view);
        resolve(state.visitedViews);
      });
    },
    delVisitedView({ commit, state }, view) {
      return new Promise(resolve => {
        commit("DEL_VISITED_VIEW", view);
        resolve(state.visitedViews);
      });
    },
    delOthersViews({ dispatch, state }, view) {
      return new Promise(resolve => {
        dispatch("delOthersVisitedViews", view);
        resolve(state.visitedViews);
      });
    },
    delOthersVisitedViews({ commit, state }, view) {
      return new Promise(resolve => {
        commit("DEL_OTHERS_VISITED_VIEWS", view);
        resolve(state.visitedViews);
      });
    }
  }
};
